package vampirism.core.client;

import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.ui.Window;
import com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldListener;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener.ChangeEvent;



public class MenuLogin {
	
    public Window window;
    public final Button confirmButton;
    public MainGame main;
    
    public MenuLogin(final MainGame main){
    	
    	this.main = main;
    	
    	System.out.println("#CLIENT: showing login interface");
    	
	    confirmButton = new TextButton("Confirm", main.skin);
	    
	    final Label titleLabel1 = new Label("Welcome to Dynautia!", main.skin);
	    final Label titleLabel2 = new Label("Please Login/Register:", main.skin);
	
	    final Label usernameLabel = new Label("E-mail: ", main.skin);
	    final Label passwordLabel = new Label("Password: ", main.skin);
	    final TextField usernameTextField = new TextField("test@test.test", main.skin);
	    final TextField passwordTextField = new TextField("test", main.skin);
	    usernameTextField.setFocusTraversal(true);
	    passwordTextField.setFocusTraversal(true);
	    passwordTextField.setPasswordCharacter('*');
	    passwordTextField.setPasswordMode(true);
	
	    window = new Window("", main.skin);
	    window.setMovable(false);
	    window.setBounds(250, 200, 300, 200);
	    window.defaults().spaceRight(20);
	    window.defaults().spaceLeft(20);
	    window.defaults().spaceTop(20);
	    window.defaults().spaceBottom(20);
	    window.row().fill().expandX();
	    window.row();
	    window.add(titleLabel1).colspan(5);
	    window.row();
	    window.add(titleLabel2).colspan(5);
	    window.row();
	    window.add(usernameLabel).colspan(2).padLeft(-2);
	    window.add(usernameTextField).minWidth(100).colspan(3).width(200).padRight(25);
	    window.row();
	    window.add(passwordLabel).colspan(2).padLeft(-2);
	    window.add(passwordTextField).minWidth(100).colspan(3).width(200).padRight(25);
	    window.row();
	    window.add(confirmButton).colspan(5).padBottom(10);
	    window.pack();
	    
	    window.setPosition((main.W-window.getWidth())/2.0f, (main.H-window.getHeight())/2.0f);
	    
        usernameTextField.setTextFieldListener(new TextFieldListener() {
            public void keyTyped (TextField textField, char key) {
            	
                    if ((int)key == 13){//Enter was pressed
                    	textField.next(false);
                    }
            }
        });
    
	    passwordTextField.setTextFieldListener(new TextFieldListener() {
	        public void keyTyped (TextField textField, char key) {
	        	
	                if ((int)key == 13){//Enter was pressed
	                	System.out.println("logging in!");
	                	confirmButton.fire(new ChangeEvent());
	                }
	        }
		});
	    
	    confirmButton.addListener(new ChangeListener() {
            public void changed(ChangeEvent event, Actor actor) {
            	
            	confirmButton.setDisabled(true);
            	main.clientSide.simulateLogin(usernameTextField.getText(), passwordTextField.getText());
            	

            }
	    });
    }
    
}
